package cn.bytecollege.user.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.User;

import java.util.List;
import java.util.Objects;

public class UserDao extends BaseDao<User,Integer> {
    /**
     *根据用户名查询用户信息
     */
    public User findUserByUsername(String username){
        String sql ="select username,user_password,user_gender,user_id,user_gender,user_nickname from user where username = ?";
        List<User> list = helper.executeQuery(sql,User.class,username);
        User user = null;
        //判断数据库里是否有数据
        if (list.size()>0){
            user = list.get(0);
        }
        return user;
    }

    /**
     * 注册用户
     */
      public int userSave(User user){
        String sql = "insert into user (username,user_password,user_phonenumber,create_time,user_head_portrait,user_gender,user_nickname) "+
                "values(?,?,?,?,?,?,?)";
        Object[] objects = {user.getUsername(),user.getUserPassword(),user.getUserPhonenumber(),user.getCreateTime()
        ,user.getUserHeadPortrait(),user.getUserGender(),user.getUserNickname()};
        return helper.executeUpdate(sql,objects);
    }
    public int updateUser(User user){
          String sql = "update user set username=?,user_password=?,user_phonenumber=?,user_head_protrait=?,user_gender=?"
                  +"where user_id=?";
          Object[] objects ={user.getUsername(),user.getUserPassword(),user.getUserPhonenumber(),user.getUserHeadPortrait()
          ,user.getUserGender()};
          return  helper.executeUpdate(sql,user);
    }

    /**
     * 根据userId查询user信息
     * @param userId
     * @return
     */
    public User findUserByUserId(Integer userId){
          String sql ="select user_Id,username,user_nickname,user_phonenumber,user_gender,user_head_portrait from user where user_id = ?";
        List<User> list = helper.executeQuery(sql,User.class,userId);
        User user = null;
        //判断数据库里是否有数据
        if (list.size()>0){
            user = list.get(0);
        }
        return user;
    }

    /**
     * 根据id修改用户信息
     * @param user
     * @return
     */
    public int userUpdateById(User user){
        String sql = "update user set user_nickname = ? ,user_phonenumber= ? ,user_gender= ?   where user_id = ? ";
        Object [] objects = {user.getUserNickname(),user.getUserPhonenumber(),user.getUserGender(),user.getUserId()};
        return helper.executeUpdate(sql,objects);
    }
}
